<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  策略模式：
  将一个一个的算法封装起来使用

  <script>
    // 策略类 采用抽象对象方式
    /* 
    var performanceS = function(){}
    performanceS.prototype.calculate = function(salary) {
      return salary * 4;
    }

    var performanceA = function(){}
    performanceA.prototype.calculate = function(salary) {
      return salary * 3;
    }
    // 新添加策略直接加入就好 不需要动Bonus类 
    var performanceC = function(){}
    performanceC.prototype.calculate = function(salary) {
      return salary * 2;
    }

    // 奖金类
    var Bonus = function() {
      this.salary = null;
      this.strategy = null;
    };

    Bonus.prototype.setSalary = function(salary) {
      this.salary = salary;
    };
    Bonus.prototype.setStrategy = function(strategy) {
      this.strategy = strategy;
    };
    Bonus.prototype.getBonus = function(strategy) {
      return this.strategy.calculate(this.salary);
    }

    var bonus1 = new Bonus();
    bonus1.setSalary(10000);
    bonus1.setStrategy(new performanceS());
    console.log(bonus1.getBonus());

    var bonus2 = new Bonus();
    bonus2.setSalary(200);
    bonus2.setStrategy(new performanceA());
    console.log(bonus2.getBonus());
 */

    // 非面向对象的策略模式实现方法
    var strategies = {
      S: function(salary) {
        return salary * 4
      },
      A: function (salary) {
        return salary * 3
      },
      B: function(salary) {
        return salary * 2
      }
    }

    var calculateBonus = function(performanceLevel, salary) {
      return strategies[performanceLevel](salary)
    }

    console.log(calculateBonus("S", 2000));

  </script>
</body>

</html>